package airacrobats.io;

import airacrobats.game.object.Airplane;
import airacrobats.io.data.MovementData;
import airacrobats.io.data.RadioData;
import airacrobats.io.data.RouteData;
import airacrobats.io.data.StageData;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

/**
 * This class loads stages from the stage file.
 */
public class StageLoader extends DataLoader<StageData>
{
    /**
     */
    protected StageData load(DataInputStream dataInputStream) throws IOException
    {
        String name = readString();
        String description = readString();

        int nRoutes = dataInputStream.readByte();
        List<RouteData> routes = new ArrayList<RouteData>();
        while (nRoutes-- > 0)
        {
            Point3f startPosition = new Point3f(dataInputStream.readFloat(),
                    dataInputStream.readFloat(), dataInputStream.readFloat());

            Vector3f startSpeed = new Vector3f(dataInputStream.readFloat(),
                    dataInputStream.readFloat(), dataInputStream.readFloat());

            int nMovements = dataInputStream.readChar();
            List<MovementData> movements = new ArrayList<MovementData>();
            for (int i = 0; i < nMovements; ++i)
            {
                movements.add(new MovementData(
                        dataInputStream.readInt(), 
                        dataInputStream.readByte(), Airplane.MAX_ROLL,
                        dataInputStream.readByte(), Airplane.MAX_PITCH,
                        dataInputStream.readByte(), dataInputStream.readBoolean()));
            }

            boolean inverted = dataInputStream.readBoolean();
            
            RouteData routeData = new RouteData(startPosition, startSpeed, movements, inverted);

            routes.add(routeData);
        }

        int neededScore = dataInputStream.readInt();
        int duration = dataInputStream.readChar();

        int radioCount = dataInputStream.readChar();
        List<RadioData> radios = new ArrayList<RadioData>();
        while (radioCount-- > 0)
        {
            int index = dataInputStream.readChar();
            int time = dataInputStream.readInt();
            radios.add(new RadioData(index, time));
        }

        return new StageData(name, description, routes, radios, neededScore, duration);
    }
}
